package ngames.com.sushitv.temp.Module;

import android.os.Handler;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;

import com.example.myvolley.MyJsonObjectRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.temp.Class.Banner;
import ngames.com.sushitv.temp.Class.Featured;

/**
 * Created by edmond on 16-7-29.
 */
public class FeaturedFractory {
    private List<Banner> banners = new ArrayList<>();
    private List<Featured> featuredArray = new ArrayList<>();

    public void initData(AppApplication application,final Handler handler){
        Map<String,String> stringRequest = new HashMap();
        stringRequest.put("position","5");
        stringRequest.put("size","4");
        stringRequest.put("uuid","xxx");
        stringRequest.put("timestamp","1470104998");
        stringRequest.put("device","android");
        stringRequest.put("api_version","1.0");
        stringRequest.put("app_version","1.0");

        MyJsonObjectRequest jsonObjectRequest = new MyJsonObjectRequest("http://60.191.1.98:9080/v1/banner", stringRequest, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d("AnimeFactoryInit", String.valueOf(response));
                try {
                    JSONArray jsonArray = response.getJSONArray("data");
                    List<Banner> bannerList = JSON.parseArray(jsonArray.toString(),Banner.class);
                    Log.d("FeaturedFactoryInit", String.valueOf(bannerList.size()));
                    banners.clear();
                    banners.addAll(bannerList);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                handler.sendEmptyMessage(0);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        if(application.queue == null){
            application.queue = Volley.newRequestQueue(application);
        }

        application.queue.add(jsonObjectRequest);
    }

    public void refreshData(final Handler handler){
//        new Thread(){
//            @Override
//            public void run() {
//                super.run();
//                try {
//                    Thread.sleep(1000);
//                    bannerUrls = new ArrayList<String>();
//                    bannerUrls.add("http://h.hiphotos.baidu.com/image/pic/item/3801213fb80e7bec43f0523e2b2eb9389a506bc2.jpg");
//                    bannerUrls.add("http://pic3.nipic.com/20090622/2605630_113023052_2.jpg");
//                    bannerUrls.add("http://b.hiphotos.baidu.com/image/pic/item/908fa0ec08fa513d17b6a2ea386d55fbb2fbd9e2.jpg");
//                    bannerUrls.add("http://g.hiphotos.baidu.com/image/pic/item/2cf5e0fe9925bc313ab004755adf8db1ca137069.jpg");
//                    Log.d("FeaturedFractory","Here");
//                    handler.sendEmptyMessage(2);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        }.start();
    }

    public List<Featured> getFeaturedArray() {
        return featuredArray;
    }

    public List<Banner> getBanners() {
        return banners;
    }
}
